package smu.KYBank;

import android.content.ContentValues;
import android.content.Context;
//import android.database.Cursor;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBHandler {
	private DBHelper helper;
	private SQLiteDatabase db;

	private DBHandler(Context ctx) {
		this.helper = new DBHelper(ctx);
		this.db = helper.getWritableDatabase();
	}

	public static DBHandler open(Context ctx) throws SQLException {
		DBHandler handler = new DBHandler(ctx);

		return handler;
	}

	public void close() {
		helper.close();
	}

	public long insert(int money) {
		ContentValues values = new ContentValues();
		values.put("member", "nol");
		//values.put("id", 1);
		
		values.put("balance", money);

		return db.insert("member", null, values);
	}
	//id VARCHAR(20) PRIMARY KEY, pw INT, name VARCHAR(20), regi_num INT, phone INT, addr TEXT,account_num INT,balance int );
	public long insert(String id, int pw, String name, int regi_num, int phone, String addr, int account_num, int balance) {
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("pw", pw);
		values.put("name", name);
		values.put("regi_num", regi_num);
		values.put("phone", phone);
		values.put("addr", addr);
		values.put("account_num", account_num);
		values.put("balance", balance);
	
		return db.insert("member", null, values);
	}
	
	public long io_cash(int account_num,int money) {
		ContentValues values = new ContentValues();
		values.put("balance", money);
		

		return db.update("member", values, "account_num=" + account_num, null);
	}
	
	public Cursor select_acnt(int account_num) throws SQLException {
		Cursor cursor = db.query(true, "member", new String[] {"id","pw","name","regi_num","phone","addr","account_num","balance"
				}, "account_num" + "=" + account_num, null, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}
	public Cursor select_id(String name) throws SQLException {
		Cursor cursor = db.query(true, "member", new String[] {"id","pw","name","regi_num","phone","addr","account_num","balance"
				}, "id" + "='" + name+"'", null, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}
	
	
}

